﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HyperLocal.Core;
using HyperLocal.Service;
using System.Globalization;
using System.Security.Principal;
using System.Web.Security;
using System.Web.UI;
using HyperLocal.MemberShipServices;


namespace HyperLocal.MVC.Controllers.Helpers
{

    public class AdminVerification : FilterAttribute, IAuthorizationFilter
    {
        public AdminTypes AdminType
        {
            get;
            set;
        }

        #region IAuthorizationFilter Members

        public void OnAuthorization(AuthorizationContext filterContext)
        {            
            if (HttpContext.Current.Session["UserName"] == null || !CurrentUserIsAdmin(HttpContext.Current.Session["UserName"].ToString()))
            {
                filterContext.HttpContext.Response.Redirect("/");
            }
        }

        #endregion

        protected bool CurrentUserIsAdmin(string userName)
        {
            
            Repository repo = ConnectionService.getRepository;

            var CurrentUser = repo.First<LocalOrbUser>(x => userName.ToUpper() == x.UserName.ToUpper());

            return CurrentUser.IsAdmin;

        }
    }
}
